<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Request\AffiliatePidRequest;
use App\CustHub\Service\AffiliatePidService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: "ch/affiliate/pid"),ApiAuth,VerifySignature]
class AffiliatePidController extends BaseController
{
    #[Inject]
    protected AffiliatePidService $service;

    #[PostMapping('list')]
    public function list(AffiliatePidRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data = $request->all();
        $data['select'] = 'id,pid,remark,oauth_id';
        $data['pageSize'] = (int)($data['page_size'] ?? 10);
        return $this->success($this->service->getPageList($data));
    }

    /**
     * 新增
     * @param AffiliatePidRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('save')]
    public function save(AffiliatePidRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->save($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 编辑
     * @param AffiliatePidRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('update')]
    public function update(AffiliatePidRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->update($this->request->input('id'),$request->all()) ? $this->success() : $this->error();
    }

    /**
     * 删除
     * @param AffiliatePidRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('delete')]
    public function delete(AffiliatePidRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->deletePid($request->input('id')) ? $this->success() : $this->error();
    }

    /**
     * 获取推广位授权备案链接
     * @param AffiliatePidRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getOauthUrl')]
    public function getOauthUrl(AffiliatePidRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success(['url' => $this->service->getOauthUrl($request->input('id'))]);
    }

    #[PostMapping('checkPidHasOauth')]
    public function checkPidHasOauth(AffiliatePidRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $rst = $this->service->checkPidHasOauth($request->all());
        return $this->success(['status' => $rst ? 1 : 0]);
    }
}